﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace ThunderboltCustomControlLibrary;

/// <summary> 选择下载位置控件 </summary>
//todo:感觉直接继承ComboBox比较简单
public class SelectDownloadPathControl:Control
{
	static SelectDownloadPathControl()
	{
		DefaultStyleKeyProperty.OverrideMetadata(typeof(SelectDownloadPathControl),
			new FrameworkPropertyMetadata(typeof(SelectDownloadPathControl)));
	}

	#region 依赖属性

	[Category("Custom attribute"), DescriptionAttribute("Gets or sets the path description header")]
	public string Title
	{
		get { return (string)GetValue(TitleProperty); }
		set { SetValue(TitleProperty, value); }
	}

	public static readonly DependencyProperty TitleProperty =
		DependencyProperty.Register("Title", typeof(string), typeof(SelectDownloadPathControl), new PropertyMetadata("电脑"));

	[Category("Custom attribute"), DescriptionAttribute("Gets or sets the selected save path")]
	public string SelectedPath
	{
		get { return (string)GetValue(SelectedPathProperty); }
		set { SetValue(SelectedPathProperty, value); }
	}

	public static readonly DependencyProperty SelectedPathProperty =
		DependencyProperty.Register("SelectedPath", typeof(string), typeof(SelectDownloadPathControl), new PropertyMetadata("未选择路径"));

	
	[Category("Custom attribute"), DescriptionAttribute("Gets or sets the remaining capacity of the selected save path")]
	public string Capacity
	{
		get { return (string)GetValue(CapacityProperty); }
		set { SetValue(CapacityProperty, value); }
	}

	public static readonly DependencyProperty CapacityProperty =
		DependencyProperty.Register("Capacity", typeof(string), typeof(SelectDownloadPathControl), new PropertyMetadata(""));


	[Category("Custom attribute"), DescriptionAttribute("Gets or sets whether to check")]
	public bool Checked
	{
		get { return (bool)GetValue(CheckedProperty); }
		set { SetValue(CheckedProperty, value); }
	}

	public static readonly DependencyProperty CheckedProperty =
		DependencyProperty.Register("Checked", typeof(bool), typeof(SelectDownloadPathControl), new PropertyMetadata(false));
	#endregion
	private void BtnOpenFile_Click(object sender, RoutedEventArgs e)
	{
		//定义传递参数
		// RoutedPropertyChangedEventArgs<Object> args = new RoutedPropertyChangedEventArgs<Object>("1", "2", ControlLoadOverEvent);
		RoutedEventArgs args = new RoutedEventArgs(ControlLoadOverEvent, this);
		//引用自定义路由事件
		this.RaiseEvent(args);

	}
	/// <summary>
	/// 声明路由事件
	/// 参数:要注册的路由事件名称，路由事件的路由策略，事件处理程序的委托类型(可自定义)，路由事件的所有者类类型
	/// </summary>
	public static readonly RoutedEvent ControlLoadOverEvent = EventManager.RegisterRoutedEvent("ControlLoadOverEvent", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventArgs<Object>), typeof(SelectDownloadPathControl));
	/// <summary>
	/// 处理各种路由事件的方法 
	/// </summary>
	public event RoutedEventHandler ControlLoadOver
	{
		//将路由事件添加路由事件处理程序
		add { AddHandler(ControlLoadOverEvent, value); }
		//从路由事件处理程序中移除路由事件
		remove { RemoveHandler(ControlLoadOverEvent, value); }
	}
	public override void OnApplyTemplate()
	{
		base.OnApplyTemplate();
		var btn = this.Template.FindName("BtnOpenFile", this) as Button;

		var tbn = this.Template.FindName("PART_ToggleButton", this) as ToggleButton;
		tbn.IsChecked = false;
		//checkbox没有勾选的话只有Title
		if (!this.Checked)
		{
			this.Opacity = 1;
			this.Capacity= string.Empty;
			this.SelectedPath = string.Empty;
		}

		btn.Click += 
			BtnOpenFile_Click;
		

	}


	/// <summary>
	/// 下拉框资源
	/// </summary>
	public ObservableCollection<string> HistoricalRecordCollection
	{
		get { return (ObservableCollection<string>)GetValue(HistoricalRecordCollectionProperty); }
		set { SetValue(HistoricalRecordCollectionProperty, value); }
	}

	public static readonly DependencyProperty HistoricalRecordCollectionProperty = DependencyProperty.Register("HistoricalRecordCollection", typeof(ObservableCollection<string>), typeof(SelectDownloadPathControl), new PropertyMetadata(new ObservableCollection<string>(), OnHistoricalRecordCollectionChanged));

	private static void OnHistoricalRecordCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
	{
		if (e.NewValue is ObservableCollection<string> newHistoricalRecordCollection)
		{
			newHistoricalRecordCollection.CollectionChanged += HistoricalRecordCollection_CollectionChanged;
		}
		//DoSomething
	}

	private static void HistoricalRecordCollection_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
	{
		//DoSomething
	}
	//todo:选择功能未添加
}